
package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.MaterialBidCert;
import com.scs.application.modules.base.mapper.MaterialBidCertMapper;
import com.scs.application.modules.base.request.MaterialBidCertQueryRequest;
import com.scs.application.modules.base.service.MaterialBidCertService;
import com.scs.application.modules.base.vo.MaterialBidCertVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 耗材报价资质 service 实现
 *
 */
@Slf4j
@AllArgsConstructor
@Service
@Transactional(rollbackFor = Exception.class)
public class MaterialBidCertServiceImpl extends BaseServiceImpl<MaterialBidCertMapper, MaterialBidCert> implements MaterialBidCertService {


    @Override
    public boolean removeCascadeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public List<MaterialBidCert> list(MaterialBidCertQueryRequest request) {
        List<MaterialBidCert> list = this.list(getQueryWrapper(request));
        return list;
    }

    @Override
    public IPage<MaterialBidCert> page(MaterialBidCertQueryRequest request) {
        IPage page = this.page(request.getPage(), getQueryWrapper(request));
        return page;
    }

    @Override
    public List<MaterialBidCertVO> listItem(MaterialBidCertQueryRequest request) {
        if (UserUtils.isSupplier()) {
            request.setSupplierId(UserUtils.getSupplierId());
        } else if (UserUtils.isHospital()) {
            request.setHospitalId(UserUtils.getHospitalId());
        }
        //request.setSupplierId(UserUtils.getSupplierId());
        return this.baseMapper.listItem(request);
    }

    @Override
    public boolean saveForMaterialBid(MaterialBidCert entity) {

        //判断资质是否已上传
//        List<MaterialBidCert> list = this.list(Wrappers.<MaterialBidCert>query()
//                .eq("cert_id", entity.getCertId())
//                .eq("bid_id", entity.getBidId()));
//
//        //查询该资质是否已经上传
//        if (list.size() != 0) {
//            throw new BusinessException("所选资质已经上传过，请重新选择");
//        }
        return this.saveOrUpdate(entity);
    }

    private QueryWrapper<MaterialBidCert> getQueryWrapper(MaterialBidCertQueryRequest request) {
        return Wrappers.<MaterialBidCert>query()
                .eq(StringUtils.isNotBlank(request.getCertType()), "cert_type", request.getCertType())
                .eq(StringUtils.isNotBlank(request.getBusName()), "bus_name", request.getBusName())
                .orderByDesc("gmt_modified");
    }
}
